﻿using AutoMapper;
using FarmSmartSleep.Common.Domain.BeginTable;
using FarmSmartSleep.Common.Domain.PM;
using FarmSmartSleep.Common.Dtos;
using FarmSmartSleep.Common.Infrastructure;
using MediatR;
using System.ComponentModel.DataAnnotations;
using System.Transactions;

namespace FarmSmartSleep.Organ.API.Application.ProductionManagement.MotherGivingBirthFile
{
    /// <summary>
    /// 添加种母分娩
    /// </summary>
    public class MotherGivingBirthCreateHandler : IRequestHandler<MotherGivingBirthCreateCommon, Result>
    {

        private readonly IMapper _mapper;
        private readonly IBaseRepository<MotherGivingBirth> motherGivinBirthRep;
        private readonly IBaseRepository<SleepInfor> sleepinforRep;

        /// <summary>
        /// 种母分娩
        /// </summary>
        /// <param name="mapper"></param>
        /// <param name="motherGivinBirthRep"></param>
        /// //在构造函数中添加了 ArgumentNullException 来确保 mapper 和 motherGivingBirthRep 不为空，这有助于避免在对象创建后由于依赖项为空而导致的错误。
        public  MotherGivingBirthCreateHandler(IMapper mapper, IBaseRepository<MotherGivingBirth> motherGivinBirthRep, IBaseRepository<SleepInfor> _sleepinforRep)
        {
            _mapper = mapper; 
            this.motherGivinBirthRep = motherGivinBirthRep ?? throw new ArgumentNullException(nameof(motherGivinBirthRep));
            this.sleepinforRep = _sleepinforRep ?? throw new ArgumentNullException(nameof(_sleepinforRep));
        }
        /// <summary>
        /// 添加实现
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public async Task<Result> Handle(MotherGivingBirthCreateCommon request, CancellationToken cancellationToken)
        {
           
                var result = new Result { Code = (int)CodeEnum.add };
            try
            {
                //using (TransactionScope tran = new (TransactionScopeOption.Required,new TransactionOptions { IsolationLevel=IsolationLevel.Serializable}))
                //{
                    // 尝试添加数据
                    var n = await motherGivinBirthRep.AddRange(request.MotherGivingBirth);
                    // 检查添加结果
                    if (n > 0) // 假设 AddAsync 返回的 n 是影响的行数或插入的 ID
                    {
                        // 更新 SleepInfor 表的栋舍栏位
                        request.MotherGivingBirth.ForEach(x =>
                        {
                            var list = sleepinforRep.GetAll().FirstOrDefault(c => c.SleepInforId == x.EweID);
                            list.RecordcottageId = x.TransferBarn;
                            list.FieIdId = x.TransferPen;
                            list.BreedStatuId =7 ;//分娩后进入哺乳
                            sleepinforRep.Update(list);
                        });

                        result.Code = (int)CodeEnum.success;
                        result.Messege = "添加成功";
                    }
                    else
                    {
                        result.Messege = "添加失败";
                    }
                    //tran.Complete();
                    return result;
                }
              
            //}
            catch (Exception)
            {
                result.Messege = "添加异常";
                throw;
            }
            
        }
    }
}
